package com.example.example_scanmusic;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Audio.Media;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

	ArrayList<String> list=new ArrayList<String>();
	private TextView mTextView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTextView = (TextView) findViewById(R.id.textView1);
	}
	
	//扫描sdcard  别忘记加读取sdcard权限
	public void scan(View v){
		MediaScanner scanner=new MediaScanner(this);
		String path = Environment.getExternalStorageDirectory()+"";
		scanner.scanFile(new File(path), null);
	}
	
	//从数据库读取音乐文件
	public void read(View v){
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, null, null, null, null);
		boolean toFirst = cursor.moveToFirst();
		while(toFirst){
			String name=cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));
			list.add(name);
			toFirst=cursor.moveToNext();
		}
		mTextView.setText(list.toString());
		cursor.close();
	}

}
